package com.coderman.syncdb;

import com.alibaba.fastjson.JSON;
import org.apache.commons.io.FileUtils;
import org.assertj.core.util.Lists;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * description: ReadFileService <br>
 * date: 2020/9/16 22:49 <br>
 * author: coderman <br>
 * version: 1.0 <br>
 */
public class ReadFileService {
    public static void main(String[] args) {
        try {
            Map<String,List<String>> countyMap = getCountyMap();
            System.out.println(countyMap);
            File all = new File("E:\\轻蜗牛直租平台项目\\中国所有小区");
            Random random = new Random();
            int index  =1;
            for (File child : all.listFiles()){
                List<String> list = FileUtils.readLines(child,"UTF-8");
                File sqlText = new File("E:\\轻蜗牛直租平台项目\\中国所有小区-sql\\sql_"+System.currentTimeMillis()+".txt");
                List<String> listSql = new ArrayList<>();
                for (String str : list){
                    String [] array = str.split(",");
                    String provinceName = array[0];
                    String cityName = array[1];
                    String areaName = array[3].replace(" ","");
                    String areaAddress = array[4];
                    String key = provinceName+"-"+cityName;
                    if(key.equals("吉林省-沈阳市")){
                        key="辽宁省-沈阳市";
                    }
                    if(key.equals("吉林省-大连市")){
                        key="辽宁省-大连市";
                    }
                    if(key.equals("吉林省-辽阳市")){
                        key="辽宁省-辽阳市";
                    }
                    List<String> areaList = countyMap.get(key);
                    String countAddress = getcountyArea(areaAddress);

                    if(areaList != null){
                        String code = areaList.get(random.nextInt(areaList.size()))+""+random.nextInt(1000)+random.nextInt(1000)+1000;
                        StringBuilder builder = new StringBuilder("insert into china_real_area values("+index+",'");
                        builder.append(code+"','"+areaName+"','"+countAddress+"','"+areaAddress+"');");
                        listSql.add(builder.toString());
                        index++;
                    }
                }
                FileUtils.writeLines(sqlText,listSql);

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static String getcountyArea(String str){
        //[嘉定区-菊园新区]棋盘路998弄
        String [] arr = str.split("]");
        if(arr.length == 2&& arr[0].contains("-")){
           return arr[0].split("-")[1];
        }
        return "";
    }

    private static Map<String,List<String>> getCountyMap() throws IOException {
        Map<String,List<String>> map = new HashMap<>();

        List<String> stringList = FileUtils.readLines(new File("E:\\轻蜗牛直租平台项目\\county.txt"),"utf-8");
        for (String str : stringList){
            String [] arr = str.split(",");
            if(arr[1].equals("北京市")){
                String key = "北京市-北京";
                String value = arr[4];
                List<String> stringList1 = map.get(key);
                if(stringList1 == null){
                    stringList1 = new ArrayList<>();
                }
                stringList1.add(value);
                map.put(key, stringList1);
            }

            else if(arr[1].equals("天津市")){
                String key = "天津市-天津";
                String value = arr[4];
                List<String> stringList1 = map.get(key);
                if(stringList1 == null){
                    stringList1 = new ArrayList<>();
                }
                stringList1.add(value);
                map.put(key, stringList1);
            }
            else if(arr[1].equals("上海市")){
                String key = "上海市-上海";
                String value = arr[4];
                List<String> stringList1 = map.get(key);
                if(stringList1 == null){
                    stringList1 = new ArrayList<>();
                }
                stringList1.add(value);
                map.put(key, stringList1);
            }
            else if(arr[1].equals("重庆市")){
                String key = "重庆市-重庆";
                String value = arr[4];
                List<String> stringList1 = map.get(key);
                if(stringList1 == null){
                    stringList1 = new ArrayList<>();
                }
                stringList1.add(value);
                map.put(key, stringList1);
            }else {
                String key = arr[1].trim()+"-"+arr[3].trim();
                String value = arr[4];
                List<String> stringList1 = map.get(key);
                if(stringList1 == null){
                    stringList1 = new ArrayList<>();
                }
                stringList1.add(value);
                map.put(key, stringList1);
            }

        }
        Lists.newArrayList("4454");
        map.put("广东省-中山市",Lists.newArrayList("4454"));
        map.put("吉林省-铁岭市",Lists.newArrayList("2225"));
        map.put("广东省-东莞市",Lists.newArrayList("4455"));
        map.put("深圳市-深圳",Lists.newArrayList("6601"));

        map.put("江苏省-昆山市",Lists.newArrayList("3214"));
        return map;


    }
}
